{% extends 'base.html' %}
{% load static %}

{% block title %}{{ device.name }} - 设备详情 - IoT智能家居控制系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-lg-8">
        <!-- 设备基本信息 -->
        <div class="card mb-4">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0">
                    <i class="bi bi-cpu me-2"></i>{{ device.name }}
                    <span class="badge bg-{% if device.status == 'online' %}success{% elif device.status == 'offline' %}danger{% else %}warning{% endif %} ms-2">
                        {% if device.status == 'online' %}在线{% elif device.status == 'offline' %}离线{% else %}未知{% endif %}
                    </span>
                </h5>
                <div class="btn-group">
                    <a href="{% url 'devices:device_edit' device.id %}" class="btn btn-outline-primary btn-sm">
                        <i class="bi bi-pencil me-1"></i>编辑
                    </a>
                    <a href="{% url 'devices:device_list' %}" class="btn btn-outline-secondary btn-sm">
                        <i class="bi bi-arrow-left me-1"></i>返回
                    </a>
                </div>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6">
                        <table class="table table-borderless">
                            <tr>
                                <td class="fw-bold text-muted">设备品牌：</td>
                                <td>{{ device.brand.name }}</td>
                            </tr>
                            <tr>
                                <td class="fw-bold text-muted">设备类型：</td>
                                <td>{{ device.device_type.name }}</td>
                            </tr>
                            <tr>
                                <td class="fw-bold text-muted">设备型号：</td>
                                <td>{{ device.model|default:"未设置" }}</td>
                            </tr>
                            <tr>
                                <td class="fw-bold text-muted">设备位置：</td>
                                <td>{{ device.location|default:"未设置" }}</td>
                            </tr>
                        </table>
                    </div>
                    <div class="col-md-6">
                        <table class="table table-borderless">
                            <tr>
                                <td class="fw-bold text-muted">MAC地址：</td>
                                <td>{{ device.mac_address|default:"未设置" }}</td>
                            </tr>
                            <tr>
                                <td class="fw-bold text-muted">IP地址：</td>
                                <td>{{ device.ip_address|default:"未设置" }}</td>
                            </tr>
                            <tr>
                                <td class="fw-bold text-muted">添加时间：</td>
                                <td>{{ device.created_at|date:"Y-m-d H:i:s" }}</td>
                            </tr>
                            <tr>
                                <td class="fw-bold text-muted">最后更新：</td>
                                <td>{{ device.updated_at|date:"Y-m-d H:i:s" }}</td>
                            </tr>
                        </table>
                    </div>
                </div>
                {% if device.description %}
                <div class="mt-3">
                    <h6 class="text-muted">设备描述</h6>
                    <p class="mb-0">{{ device.description }}</p>
                </div>
                {% endif %}
            </div>
        </div>

        <!-- 设备控制命令 -->
        <div class="card mb-4">
            <div class="card-header">
                <h6 class="mb-0"><i class="bi bi-controller me-2"></i>设备控制</h6>
            </div>
            <div class="card-body">
                {% if device.device_type.commands.exists %}
                    <div class="row g-3">
                        {% for command in device.device_type.commands.all %}
                        <div class="col-md-4">
                            <div class="card border-light">
                                <div class="card-body text-center">
                                    <h6 class="card-title">{{ command.name }}</h6>
                                    <p class="card-text small text-muted">{{ command.description }}</p>
                                    <button class="btn btn-primary btn-sm" onclick="executeCommand('{{ command.id }}', '{{ command.name }}')">
                                        <i class="bi bi-play-circle me-1"></i>执行
                                    </button>
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                {% else %}
                    <div class="text-center text-muted py-4">
                        <i class="bi bi-exclamation-circle fs-1"></i>
                        <p class="mt-2">该设备类型暂无可用控制命令</p>
                    </div>
                {% endif %}
            </div>
        </div>
    </div>

    <!-- 侧边栏 -->
    <div class="col-lg-4">
        <!-- 快速操作 -->
        <div class="card mb-4">
            <div class="card-header">
                <h6 class="mb-0"><i class="bi bi-lightning me-2"></i>快速操作</h6>
            </div>
            <div class="card-body">
                <div class="d-grid gap-2">
                    <button class="btn btn-outline-success" onclick="refreshStatus()">
                        <i class="bi bi-arrow-clockwise me-1"></i>刷新状态
                    </button>
                    <button class="btn btn-outline-info" onclick="testConnection()">
                        <i class="bi bi-wifi me-1"></i>测试连接
                    </button>
                    <button class="btn btn-outline-warning" onclick="resetDevice()">
                        <i class="bi bi-arrow-counterclockwise me-1"></i>重启设备
                    </button>
                </div>
            </div>
        </div>

        <!-- 设备统计 -->
        <div class="card mb-4">
            <div class="card-header">
                <h6 class="mb-0"><i class="bi bi-graph-up me-2"></i>使用统计</h6>
            </div>
            <div class="card-body">
                <div class="row text-center">
                    <div class="col-6">
                        <div class="border-end">
                            <h4 class="text-primary mb-1">{{ device.commands.count }}</h4>
                            <small class="text-muted">执行命令</small>
                        </div>
                    </div>
                    <div class="col-6">
                        <h4 class="text-success mb-1">
                            {% if device.status == 'online' %}99.9%{% else %}0%{% endif %}
                        </h4>
                        <small class="text-muted">在线率</small>
                    </div>
                </div>
            </div>
        </div>

        <!-- 相关场景 -->
        <div class="card">
            <div class="card-header">
                <h6 class="mb-0"><i class="bi bi-collection me-2"></i>相关场景</h6>
            </div>
            <div class="card-body">
                {% if device.scene_devices.exists %}
                    {% for scene_device in device.scene_devices.all %}
                    <div class="d-flex justify-content-between align-items-center mb-2">
                        <span>{{ scene_device.scene.name }}</span>
                        <a href="#" class="btn btn-outline-primary btn-sm">查看</a>
                    </div>
                    {% endfor %}
                {% else %}
                    <p class="text-muted mb-0">暂无相关场景</p>
                {% endif %}
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
// 执行设备命令
function executeCommand(commandId, commandName) {
    if (!confirm('确定要执行 "' + commandName + '" 命令吗？')) {
        return;
    }
    
    fetch('/api/devices/{{ device.id }}/control/', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': document.querySelector('[name=csrfmiddlewaretoken]').value
        },
        body: JSON.stringify({
            command_id: commandId
        })
    })
    .then(function(response) { return response.json(); })
    .then(function(data) {
        if (data.success) {
            alert('命令执行成功：' + data.message);
        } else {
            alert('命令执行失败：' + data.message);
        }
    })
    .catch(function(error) {
        console.error('Error:', error);
        alert('命令执行失败，请稍后重试');
    });
}

// 刷新设备状态
function refreshStatus() {
    fetch('/api/devices/{{ device.id }}/status/', {
        method: 'GET',
        headers: {
            'X-CSRFToken': document.querySelector('[name=csrfmiddlewaretoken]').value
        }
    })
    .then(function(response) { return response.json(); })
    .then(function(data) {
        if (data.success) {
            location.reload();
        } else {
            alert('状态刷新失败：' + data.message);
        }
    })
    .catch(function(error) {
        console.error('Error:', error);
        alert('状态刷新失败，请稍后重试');
    });
}

// 测试设备连接
function testConnection() {
    alert('连接测试功能开发中...');
}

// 重启设备
function resetDevice() {
    if (!confirm('确定要重启设备吗？')) {
        return;
    }
    alert('设备重启功能开发中...');
}
</script>
{% endblock %}